package com.ml.mall.controller.banner;

import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.banner.*;
import com.ml.mall.bean.res.banner.BannerQueryByTypeRes;
import com.ml.mall.bean.res.banner.BannerQueryRes;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.service.banner.SysBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author :pl
 * @version :1.0
 * @description :banner相关接口
 * @date :2021/5/21
 **/

@Api(tags = "【dim】banner相关接口")
@RestController
@RequestMapping("/banner")
public class SysBannerController {
    @Autowired
    private SysBannerService sysBannerService;
    @ApiOperation(value = "banner图添加")
    @PostMapping("/new")
//    @RequiresPermissions(PermissionConst.BANNER_NEW)
    public ApiRestResult addBanner(@RequestBody @Valid SysBannerAddDto dto){
        int i = sysBannerService.addBanner(dto);
        if (i==0){
            return ApiRestResult.err();
        }
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "banner图批量逻辑删除")
    @PostMapping("/del")
//    @RequiresPermissions(PermissionConst.BANNER_DEL)
    public ApiRestResult delBanner(@RequestBody SysBannerDelDto dto){
        int i = sysBannerService.delBanner(dto);
        if (i==0){
            return ApiRestResult.err();
        }
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "查询banner图：用于商城查询")
    @PostMapping("/query")
//    @RequiresPermissions(PermissionConst.BANNER_QUERY)
    public ApiRestResult queryBanner(){
        BannerQueryRes bannerQueryRes = sysBannerService.queryBanner();
        if (bannerQueryRes==null){
            return ApiRestResult.err();
        }
        return ApiRestResult.ok(bannerQueryRes);
    }

    @ApiOperation(value = "banner图的信息修改")
    @PostMapping("/edit")
//    @RequiresPermissions(PermissionConst.BANNER_EDIT)
    public ApiRestResult updBanner(@RequestBody @Valid SysBannerEditDto dto){
        int i = sysBannerService.updBanner(dto);
        if (i==0){
            return ApiRestResult.err();
        }
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "增加点击量")
    @PostMapping("/click")
//    @RequiresPermissions(PermissionConst.BANNER_CLICK)
    public ApiRestResult clickBanner(@RequestBody @Valid SysBannerClickDto dto){
        int i = sysBannerService.clickBanner(dto);
        if (i==0){
            return ApiRestResult.err();
        }
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "根据类型查询对应的轮播图")
    @PostMapping("/query_by_type")
//    @RequiresPermissions(PermissionConst.BANNER_QUERY_BY_TYPE)
    public ApiRestResult queryByType(@RequestBody @Valid SysBannerQueryByTypeDto dto){
        BannerQueryByTypeRes queryByTypeRes = sysBannerService.queryByType(dto);
        return ApiRestResult.ok(queryByTypeRes);
    }
}
